var savePath = "";

// *******************table 数字校验*******************************
function vailFloatNumberLimitDecimalPoint(evnt, obj) {
	evnt = evnt || window.event;
	var keyCode = window.event ? evnt.keyCode : evnt.which;
	if (obj.value.indexOf(".") != -1 && keyCode == 46)
		return false;
	return keyCode >= 48 && keyCode <= 57 || keyCode == 46 || keyCode == 8;
}

// *****************************************************
function getUrl(tag, url) {
	$('#contractBrand').attr("src", url);
}
// *****************************************************
function changeSelect(id, value) {
	if (id == "#guideType" && value == 1) {
		$('#guideAmount').removeAttr("disabled");
	} else if (id == "#guideType" && value == 0) {
		$('#guideAmount').attr("disabled", "disabled");
	}
	$("" + id + "").val(value);
}

// *****************************************************

var  list = Array();
var itemList = Array();
var assbaseItemsList = Array();
var _removeBarndList = Array();
var _removeAssBaseList = Array();
var _removeAssitemsList = Array();
/** ************删除数组************* */
Array.prototype.remove = function(dx) {

	if (isNaN(dx) || dx > this.length) {
		return false;
	}
	for (var i = 0, n = 0; i < this.length; i++) {
		if (this[i] != this[dx]) {
			this[n++] = this[i]
		}
	}
	this.length -= 1
}
/** *********************** */
function edit_assitems(t, rowIndex) {
	var itemsJson = "";
	var deductItem = $("#deductItem_items" + rowIndex).attr("value");
	var taxRate = $("#taxRate_items" + rowIndex).attr("value");
	var remarks = $("#remarks_items" + rowIndex).val();
	var contracttype = $("#contract_type").val();
	var supplierOption = $("#suppierOperation").val();
	itemsJson += itemsToJson(rowIndex).substr(0,itemsToJson(rowIndex).length - 1).replace(/\s+/g, '');
	var path =Com_Parameter.ContextPath + '/biz/contract/' + savePath+ '/editAssitems?itemsJson='+itemsJson+'&num='+rowIndex+'&contractType='+contracttype+'&supplierOption='+supplierOption
	$("#assitems").load(path,function() {
				 $('#deductCode', this).chosen('destroy').chosen();
				$("#assitems").modal("show");
				$("#taxRate_items").val(taxRate);
				$("#deductItem").val(deductItem);
				$("#remarks_items").val(remarks)
				$("#startDate_items").val(
						$("#startDate_items" + rowIndex).attr("value"));
				$("#endDate_items").val(
						$("#endDate_items" + rowIndex).attr("value"));

			});
}


/*******************************
 * 
 *******************************/
function getPage() {
	$("#supplierModal").load(Com_Parameter.ContextPath + "/biz/brand/supplier/suppInfoList",
	function(data) {
		$("#supplierModal").modal("show");
	});
}

/*******************************
 * 
 *******************************/
function remove_assbaseitems(t, rowIndex) {
	var assbaseitems_id = $("#assbaseitems_id" + rowIndex).attr("value");
	if (!isNaN(assbaseitems_id) || assbaseitems_id != null) {
		_removeAssBaseList.push(assbaseitems_id);
	}
	$(t).closest('tr').remove()
	assbaseItemsList.remove(rowIndex);
}
/*******************************
 * 
 *******************************/
function remove_assitems(t, rowIndex) {
	var assitems_id = $("#assitems_id" + rowIndex).attr("value");
	if (!isNaN(assitems_id) || assitems_id != "") {
		_removeAssitemsList.push(assitems_id)
	}
	$(t).closest('tr').remove()
	itemList.remove(rowIndex);
}
/*******************************
 * 
 *******************************/
/*******************************
 * 
 *******************************/
function edit_brand(t, rowIndex) {
	var barndJson = "";
	var shopId = $("#assShopIds" + rowIndex).val();
	var shopName = $("#shopNames" + rowIndex).val();
	var brandEName = $("#brandEName" + rowIndex).attr("value");
	var brandCName = $("#brandCName" + rowIndex).attr("value");
	var supplierNo =$("#supplierNo").attr("value");
	var brandId =$("#brandId"+rowIndex).attr("value");
	var iszs =$("#iszs"+rowIndex).attr("value");
	barndJson += brandToJson(rowIndex).substr(0,brandToJson(rowIndex).length - 1).replace(/\s+/g, '');
	var path =Com_Parameter.ContextPath+'/biz/contract/commonContract/editAssbrand?assBrand='+barndJson+'&num='+rowIndex+"&supplierNo="+supplierNo+"&brandId="+brandId+"&iszs="+iszs;
	$("#barndModal").load(path, function() {
		$("#barndModal").modal("show");
		$("#assShopIds").val(shopId);
		$("#shopName").val(shopName);
		$("#brandEName").val(brandEName);
		$("#brandCName").val(brandCName);

	});
}

/*******************************
 * 
 *******************************/
function remove_brand(t, rowIndex) {
	var assbrand_id = $("#assbrand_id" + rowIndex).attr("value")
	if (!isNaN(assbrand_id) || assbrand_id != null) {
		_removeBarndList.push(assbrand_id);
	}
	$(t).closest('tr').remove()
	list.remove(rowIndex);
}
/*******************************
 * 
 *******************************/
function edit_base(t, rowIndex) {
	var baseJson = "";
	baseJson += baseToJson(rowIndex).substr(0, baseToJson(rowIndex).length - 1)
			.replace(/\s+/g, '')
	var remarks = $("#remarks_base" + rowIndex).attr("value");
	var isCompare=$("#isCompare_base" + rowIndex).attr("value");
	$("#assbasemodal").load(Com_Parameter.ContextPath + '/biz/contract/' + savePath+ '/editModalBase?baseJson=' + baseJson + '&num='+ rowIndex,function() {
		$("#assbasemodal").modal("show");
		$("#remarks_base").val(remarks)
		$("#startDate_base").val($("#startDate_base" + rowIndex).attr("value"));
		$("#endDate_base").val($("#endDate_base" + rowIndex).attr("value"));
		$("#isCompare").val(isCompare);
	});
}
/*******************************
 * 
 *******************************/
function remove_assbaseitems(t, rowIndex) {
	var assbaseitems_id = $("#assbaseitems_id" + rowIndex).attr("value");
	if (!isNaN(assbaseitems_id) || assbaseitems_id != null) {
		_removeAssBaseList.push(assbaseitems_id);
	}
	$(t).closest('tr').remove()
	assbaseItemsList.remove(rowIndex);

}
/*******************************
 * 
 *******************************/
function validation() {
	var startDate =$("#startDate").val();
	var endDate =$("#endDate").val();
	
	if ($("#area").val() == "") {
		$("#area").focus();
		top.$.jBox.tip('管理区域不能为空', 'warning');
		return false;
	}
	if ($("#store").val() == "") {
		$("#store").focus();
		top.$.jBox.tip('所属分店不能为空', 'warning');
		return false;
	}
	if ($("#store").val() == "") {
		$("#store").focus();
		top.$.jBox.tip('所属分店不能为空', 'warning');
		return false;
	}
	if ($("#shop").val() == "") {
		$("#shop").focus();
		top.$.jBox.tip('商铺号不能为空', 'warning');
		return false;
	}
	if ($("#signBy").val() == "") {
		$("#signBy").focus();
		top.$.jBox.tip('合同签订人不能为空', 'warning');
		return false;
	}
	if ($("#startDate").val() == "") {
		$("#startDate").focus();
		top.$.jBox.tip('合同起始时间不能为空', 'warning');
		return false;
	}
	if ($("#endDate").val() == "") {
		$("#endDate").focus();
		top.$.jBox.tip('合同终止时间不能为空', 'warning');
		return false;
	}
	if ($("#endDate").val() <= $("#startDate").val()) {
		$("#endDate").focus();
		top.$.jBox.tip('合同终止时间必须大于合同开始时间,请重新输入!', 'warning');
		return false;
	}

	if ($("#supplierId").val() =="") {
		$("#supplierName").focus();
		top.$.jBox.tip('供应商不能为空,请重新输入!', 'warning');
		return false;
	}
	
	if (savePath != "advertassociated") { // 如果广告合同则不验证

		if ($("#contractArea").val() == "") {
			
			$("#contractArea").focus();
			top.$.jBox.tip('合同签订面积不能为空', 'warning');
			return false;
		}
		if ($("#areaCoefficient").val() == "") {
			$("#areaCoefficient").focus();
			top.$.jBox.tip('面积分摊系数不能为空', 'warning');
			return false;
		}
		
		
		if ($("#settlementCycle").val() == "") {
			$("#settlementCycle").focus();
			top.$.jBox.tip('结算周期不能为空', 'warning');
			return false;
		}
//		//如果是租赁合同,验证商户属性
//		if(savePath = "leaseassociated"){
//			if ($("#isMainShop").val() == "") {
//				$("#isMainShop").focus();
//				top.$.jBox.tip('商户属性不能为空', 'warning');
//				return false;
//			}
//	    }
		if ($("#paymentMethod").val() == "") {
			$("#paymentMethod").focus();
			top.$.jBox.tip('付款方式不能为空', 'warning');
			return false;
		}
		if (savePath != "marketassociated") {
			if ($("#suppierOperation").val() == "") {
				$("#suppierOperation").focus();
				top.$.jBox.tip('供应商经营方式不能为空', 'warning');
				return false;
			}
		}
		if ($("#purOrg").val() == "") {
			$("#purOrg").focus();
			top.$.jBox.tip('采购组织不能为空', 'warning');
			return false;
		}
		if ($("#salesVolume").val() == "") {
			$("#salesVolume").focus();
			top.$.jBox.tip('目标销售额不能为空', 'warning');
			return false;
		}

		if ($("#benefit").val() == "") {
			$("#benefit").focus();
			top.$.jBox.tip('坪效不能为空', 'warning');
			return false;
		}

		if ($("#checkoutType").val() == "") {
			$("#checkoutType").focus();
			top.$.jBox.tip('收银方式不能为空', 'warning');
			return false;
		}
		if ($("#isUndertakeElec_radio").val() == "") {
			$("#isUndertakeElec_radio").focus();
			top.$.jBox.tip('供应商用电不能为空', 'warning');
			return false;
		}
		if ($("#taxTypeCode").val() == "" && $("#taxTypeCode").val()!=null) {
			$("#taxTypeCode_select").focus();
			top.$.jBox.tip('税票类型不能为空', 'warning');
			return false;
		}
		
		
	
		//-----------------校验合同条款,合同销售分成及结算日期是否在合同开始和结束日之间----------------------------------------------
		
		for (var m = 0; m < itemList.length; m++) {
			var _itemStartDate=$("#startDate_items" + itemList[m]).attr("value");
			var _itemEndDate=$("#startDate_items" + itemList[m]).attr("value");
		
			
			if(startDate>_itemStartDate){
				top.$.jBox.tip('条款第'+Number(m+1)+'条开始开始日期大于等于合同日期', 'warning');
				return  false;
			}
			
			if(_itemEndDate>endDate){
				top.$.jBox.tip('条款第'+Number(m+1)+'条结束日期小于等于合同结束日期', 'warning');
				
				return false;
			}
		}
		
		for (var k = 0; k < assbaseItemsList.length; k++) {
			var _baseStartDate=$("#startDate_base" + assbaseItemsList[k]).attr("value");
			var _baseEndDate=$("#endDate_base" + assbaseItemsList[k]).attr("value");
			if(startDate>_baseStartDate){
				
				top.$.jBox.tip('销售分成结算第'+Number(k+1)+'条开始开始日期大于等于合同日期', 'warning');
				return false;
			}
			
			if(_baseEndDate>endDate){
				top.$.jBox.tip('销售分成结算第'+Number(k+1)+'条结束日期小于等于合同结束日期', 'warning');
				return false;
			}
		}
	}

}
//---------删除品牌-------------------------
function getRemoveBrand(){
	var remove_Barnd = "";
	for (var i = 0; i < _removeBarndList.length; i++) {
		remove_Barnd += _removeBarndList[i] + ",";

	}
	return remove_Barnd;
}


//---------获取品牌名称-------------------------
function getBrandCname(){
	var contractName="";
	//--------------------获取主品牌编码--------------------------------------;
	for (var j = 0; j < list.length; j++) {
		if($("#mainBarnd" + list[j]).attr("value") == "1"){
			contractName += $("#brandCName" + list[j]).attr("value");
		}
	}
	return contractName;
}

//---------删除销售分成及结算-------------------------
function getRemoveAssBase(){
	var remove_AssBase = "";
	for (var i = 0; i < _removeAssBaseList.length; i++) {
		remove_AssBase  +=_removeAssBaseList[i] + ",";
	}
	return remove_AssBase;
}


//-----------------删除合同信息-------------------------------------
function getDeleteContract(){
	var deleteContract="";
	var remove_Barnd = getRemoveBrand();
	var contractName =getBrandCname();
	var remove_AssBase=getRemoveAssBase();
	var remove_Assitems =getRemoveAssitems();

	$('#subject').val(contractName);
	if ($('#taxRateCode').attr("value") == null
			|| $('#taxRateCode').attr("value") == "") {
		$('#taxRateCode').val($('#taxRateCode_select').attr("value"))
	}
	if ($('#taxRate').attr("value") == null
			|| $('#taxRate').attr("value") == "") {
		$('#taxRate').val($('#taxRateCode_select').find("option:selected").text())
	}
	deleteContract = "[";
	deleteContract += "{'brand':'" + remove_Barnd + "','assBase':'"
			+ remove_AssBase + "','assitems':'" + remove_Assitems
			+ "'}";
	deleteContract += "]";
	return deleteContract;
}
function getRemoveAssitems(){
	var remove_Assitems = "";
	for (var i = 0; i < _removeAssitemsList.length; i++) {
		remove_Assitems += _removeAssitemsList[i] + ",";
	}
	return remove_Assitems;
}
/** *********************************** */
$(function() {
	var contextPath = Com_Parameter.ContextPath;

	savePath = $("#contract_type").val();
	for (var i = 0; i < num; i++) {
		list.push(i);
	}
	for (var i = 0; i < num_items; i++) {
		itemList.push(i);
	}
	for (var i = 0; i < num_base; i++) {
		assbaseItemsList.push(i);
	}

	if (savePath == "advertassociated") {
		showBerthInfo();
	}
	
	
	$("#endDate").change(function(){
		if ($("#endDate").val() <= $("#startDate").val()) {
			$("#endDate").focus();
			$("#endDate").val();
			$('.endDate').datetimepicker('setStartDate', '2012-01-01');
			top.$.jBox.tip('合同终止时间必须大于合同开始时间,请重新输入!', 'warning');
			return false;
		}
	});
	
	
	$("#startDate").change(function(){
		if($("#endDate").val()!=null&&$("#endDate").val()!=""){
			if ($("#endDate").val() <= $("#startDate").val()) {
				$("#startDate").focus();
				$("#startDate").val();
				top.$.jBox.tip('合同终止时间必须大于合同开始时间,请重新输入!', 'warning');
				return false;
			}
		}
	});
	
	$("#supplier").click(function() {
		$("#supplierModal").load(contextPath+ '/biz/brand/supplier/suppInfoPage?pageSize=10',function() {
				$("#supplierModal").modal("show");
		});
	});
	
	$("#areaCoefficient").keyup(function(){
		var str=$(this).val();
		if(parseFloat(str) >= 100){
			$(this).val("");
			//top.$.jBox.tip('面积分摊系数不能大于100', 'warning');
			return;
		}
	});
	$("#save").click(function(e) {
				var flag = validation();
				if (flag == false) {
					return;
				}
				var deleteContract=getDeleteContract();
				// **************************删除合同品牌信息************************************************************
				$('#deleteContract').val(deleteContract);
				$('#contractForm').method = "post";
				$('#contractForm').attr('action',contextPath + '/biz/contract/' +savePath+ '/save?navId=' + navId);
				$(this).attr("disabled","disabled");
				$("#contractForm").submit();
				Oosp_Loading('正在提交，请稍等...');
			});
	/***********提交**************************/
	$("#submit_workFlower").click(function(e) {
		var flag = validation();
		if (flag == false) {
			return;
		}
		var deleteContract=getDeleteContract();
		// **************************删除合同品牌信息************************************************************
		$('#deleteContract').val(deleteContract);
		
		$('#contractForm').method = "post";
		$('#contractForm').attr('action',contextPath + '/biz/contract/' +savePath+ '/submit?navId=' + navId);
		$("#contractForm").submit();
		$(this).attr("disabled","disabled");
		Oosp_Loading('正在提交，请稍等...');
		
	});
	/** ****************************** */
	/** ****************************** */
	$("a[id='assbrands']").click(
			function() {
				var supplierNo = $("#supplierNo").attr("value")
				if (supplierNo == null || supplierNo == "") {
					top.$.jBox.tip('请选择供应商', 'warning');
					return;
				}
				var shopId =$("#storeId").val()
				if (shopId == null || shopId == "") {
					top.$.jBox.tip('请选择门店', 'warning');
					return;
				}
				$("#barndModal").load(
						contextPath + '/biz/contract/commonContract/addAssbrands?supplierNo='+supplierNo, function() {
							$("#barndModal").modal("show");
						});
			});
	
	/** ****************************** */
	$("a[id='add_assitems']").click(
		function() {
			
			var shopId =$("#storeId").val()
			if (shopId == null || shopId == "") {
				top.$.jBox.tip('请选择门店', 'warning');
				return;
			}
			
			var contracttype = $("#contract_type").val();
			var supplierOption = $("#suppierOperation").val();
			if(contracttype=='selfassociated'&&(supplierOption==null||supplierOption=='')){
				top.$.jBox.tip('请选择供应商经营方式', 'warning');
				return;
			}
		
			$("#assitems").load(contextPath + '/biz/contract/'+savePath+'/addAssitems?contractType='+contracttype+'&supplierOption='+supplierOption,
					function() {
						 $('#deductCode', this).chosen('destroy').chosen();
						$("#assitems").modal("show");
						$("#startDate_items").val($("#startDate").attr("value"));
						$("#endDate_items").val($("#endDate").attr("value"));
					});
		});
	/** ****************************** */
	$("a[id='assbase']").click(
			function() {
				$("#assbasemodal").load(
						contextPath + '/biz/contract/' + savePath
								+ '/addModalBase',
						function() {
							$("#payDate_div").hide();
							$("#assbasemodal").modal("show");
							$("#startDate_base").val($("#startDate").attr("value"));
							$("#endDate_base").val($("#endDate").attr("value"));
						});
			});

	// 经营门店 多选对话框
	var selectStoreDialog = {
		service : "selectShopByRegionService",
		idField : "selfStore_storeNo",
		nameField : "selfStore_store",
		multiple : "true",
		title : "门店选择对话框"
	};
	// ------------------------------------------------------------
	$("#selectStoreDialog").Dialog(selectStoreDialog);
	$("#contractArea").decimalinput();
	$("#areaCoefficient").decimalinput();
	$("#benefit").decimalinput();
	$("#salesVolume").decimalinput();
	if (taxTypeCode != null && '' != taxTypeCode) {
		$("#taxRateCode_select").FillOptions(contextPath + "/biz/contract/commonContract/" + taxTypeCode
					+ "/getTaxRateTypeCode", {
					datatype : "json",
					textfield : "taxRate",
					valuefiled : "taxRateNum",
					defValue : taxRateCode
				});
	}
	$("#formatIdSelect").FillOptions(
			contextPath + "/biz/contract/commonContract/getLoadFormat?storeId="
					+ storeId1, {
				datatype : "json",
				textfield : "name",
				valuefiled : "code",
				defValue : formart
			});
	$("#codeTable > tbody")
			.find("tr")
			.each(
					function() {
						$(this)
								.find("td")
								.each(
										function() {
											var select = $(this).find("select");
											select
													.each(function() {
														var index = $(this)
																.attr(
																		"data_index");
														var kid = 'assTempItemCodeList'
																+ index
																+ '.itemCode';
														var did = 'assTempItemCodeList'
																+ index
																+ '.conditions';
														if (this.id == kid) {
															var ui = $(this);
															var defValue = ui
																	.attr("data_def");
															ui
																	.FillOptions(
																			contextPath
																					+ "/sys/base/spfItem/getSpecificItems",
																			{
																				datatype : "json",
																				textfield : "itemName",
																				valuefiled : "itemNum",
																				defValue : ""
																						+ defValue
																						+ ""
																			});
														}
														if (this.id == did) {
															var ui = $(this);
															var defValue = ui
																	.attr("data_def");
															var sptNum = ui
																	.attr("spt_Num");
															ui
																	.FillOptions(
																			contextPath
																					+ "/sys/base/spfItem/getConditions?sptNum="
																					+ defValue,
																			{
																				datatype : "json",
																				textfield : "value",
																				valuefiled : "key",
																				defValue : ""
																						+ defValue
																						+ ""
																			});
														}
													});
										});
					});
	// 选择地区
	var isListJsp = $("#isListJsp").val();
	if (isListJsp) {
		$("#area").val("");
		$("#areaId").val("");
		$("#contractNo").val("");
	}
	var contractType = $("#contractType").val();
	var area = $("#area").val();
	if (area != "" && area != null) {
		var areaId = $("#areaId").val();
		$.get(contextPath + '/biz/contract/commonContract/' + areaId
				+ '/getStoreByAreaId', function(data) {
			var jsonObj = eval('(' + data + ')');
			for (var i = 0; i < jsonObj.length; i++) {
				var shop = jsonObj[i];
				$("#storeId").append("<option value='" + shop.code + "'>" + shop.name+ "</option>");
				$("#storeId").children("option").each(function() {
					if ($(this).val() == storeId1) {
						$(this).attr("selected", "selected");
					}
				});
			}
		});
		$.get(contextPath + '/biz/contract/commonContract/' + storeId1
				+ '/getFloorByStoreId', function(data) {
			var jsonObj = eval('(' + data + ')');
			for (var i = 0; i < jsonObj.length; i++) {
				var floor = jsonObj[i];
				$("#floorId").append(
						"<option value='" + floor.code + "'>" + floor.name
								+ "</option>");
				$("#floorId").children("option").each(function() {
					if ($(this).val() == floorId1) {
						$(this).attr("selected", "selected");
					}
				});
			}
		});
	}
	var opt = {
		service : "selectOneRegionService",
		idField : "areaId",
		nameField : "area",
		title : "地区选择对话框"
	};
	$("#areaDialog").Dialog(opt);
	// 选取门店
	$("#area").blur(
	function() {
				var areaId = $("#areaId").val();
				$.get(contextPath + '/biz/contract/commonContract/' + areaId
						+ '/getStoreByAreaId',
						function(data) {
							var jsonObj = eval('(' + data + ')');
							$("#storeId").children("option").each(function() {
								$(this).remove();
							});
							$("#storeId").append(
									"<option value=''>---请选择---</option>");
							for (var i = 0; i < jsonObj.length; i++) {
								var shop = jsonObj[i];
								$("#storeId").append(
										"<option value='" + shop.code + "'>"
												+ shop.name + "</option>");
							}
						});
			});

	$("#floorId").change(function() {
		var floorId = $("#floorId").val();
		var floor;
		$(this).children("option").each(function() {
			if ($(this).val() == floorId) {
				floor = $(this).text();
			}
		});
		$("#floor").val(floor);
	});

	$("#serch").click(
			function() {
				$("#searchForm")
						.attr(
								"action",
								"${contextPath}/biz/contract/" + contractType
										+ "/list");
			});
	$("#shopDialog")
			.click(
					function() {
						var floorId = $("#floorId").attr("value");
						var storeId = $("#storeId").attr("value");
						if (floorId == null || floorId == '') {
							$("#floor").focus();
							top.$.jBox.tip('楼层不能为空', 'warning');
							return false;
						} else {
							$("#modalBrand")
									.load(
											contextPath
													+ "/biz/contract/commonContract/showModal?floor="
													+ floorId + "&siteid="
													+ storeId + "&pageSize=10",
											function() {

												$("#modalBrand").modal("show");
											});
						}
					});
	
//	// 选取楼层
//	$("#storeId").change(
//			function() {
//				var storeId = $("#storeId").val();
//				var store;
//				
//				
//			});
	
	$("#storeId").change(
			function() {
				var store = $("#store").val();
				var storeId = $("#storeId").val();
				
				$(this).children("option").each(function() {
					if ($(this).val() == storeId) {
						store = $(this).text();
					}
				});
				
				$.get(contextPath + "/biz/contract/commonContract/" + storeId
						+ "/getFormat", function(data) {
					var jsonObj = eval('(' + data + ')');
					var formatList = jsonObj.formatList;
					$("#formatIdSelect").children("option").each(function() {
						$(this).remove();
					});
					for (var i = 0; i < formatList.length; i++) {
						var format = formatList[i];
						if (i == 0) {
							$("#formatId").val(format.code)
						}
						$("#formatIdSelect").append(
								"<option value='" + format.code + "'>"
										+ format.name + "</option>");
					}
					var code = jsonObj.code;
					var name = jsonObj.name;
					if (name != null) {
						$("#signBodyName").val(name);
					}
					if (code != null) {
						$("#signBody").val(code);
					}
				});
				
				$("#store").val(store);
				if (storeId != "" && storeId != null) {
					$.get(contextPath + '/biz/contract/commonContract/'
							+ storeId + '/getFloorByStoreId', function(data) {
						var floorList = eval('(' + data + ')');
						$("#floorId").children("option").each(function() {
							$(this).remove();
						});
						$("#floorId").append(
								"<option value=''>---请选择---</option>");
						for (var i = 0; i < floorList.length; i++) {
							var floor = floorList[i];
							$("#floorId").append(
									"<option value='" + floor.code + "'>"
											+ floor.name + "</option>");
						}
					});
				}
			});

});

/*******************************
 * 销售分成及结算更新
 *******************************/
function updateBase(rowIndex) {
	$("#iscommGuarantee" + rowIndex).val($("#iscommGuarantee").attr("value"));
	$("#isGuarantee_base" + rowIndex).val($("#isGuarantee").attr("value"));
	$("#guaranteeType_base" + rowIndex).val($("#guaranteeType").attr("value"));
	$("#guaranteeAmount_base" + rowIndex).val(
			$("#guaranteeAmount").attr("value"));
	$("#codeType_base" + rowIndex).val($("#codeType").attr("value"));
	$("#lower_base" + rowIndex).val($("#lower").attr("value"));
	$("#upper_base" + rowIndex).val($("#upper").attr("value"));
	$("#startDate_base" + rowIndex).val($("#startDate_base").attr("value"));
	$("#endDate_base" + rowIndex).val($("#endDate_base").attr("value"));
	$("#validType_base" + rowIndex).val($("#validType_base").attr("value"));
	$("#countBase_base" + rowIndex).val($("#countbase_base").attr("value"));
	$("#deductType_base" + rowIndex).val($("#deductType_base").attr("value"));
	$("#payDate_base" + rowIndex).val($("#payDate_base").attr("value"));
	$("#remarks_base" + rowIndex).val($("#remarks_base").val());
	$("#deductRate_base" + rowIndex).val($("#deductRate").attr("value"));
	$("#barndId_base" + rowIndex).val($("#brandName").attr("value"));
	$("#isCompare_base" + rowIndex).val($("#isCompare").attr("value"));
	
	if ($("#isGuarantee").attr("value") == null
			|| $("#isGuarantee").attr("value") == "") {
		$("#isGuaranteeBase_td" + rowIndex).html("");
	} else {
		$("#isGuaranteeBase_td" + rowIndex).html(
				$("#isGuarantee option:selected").text());
	}
	if ($("#guaranteeType").attr("value") == null
			|| $("#guaranteeType").attr("value") == "") {
		$("#guaranteeTypeBase_td" + rowIndex).html("");
	} else {
		$("#guaranteeTypeBase_td" + rowIndex).html(
				$("#guaranteeType option:selected").text());
	}
	if ($("#codeType").attr("value") == null
			|| $("#codeType").attr("value") == "") {
		$("#codeTypeBase_td" + rowIndex).html("");
	} else {
		$("#codeTypeBase_td" + rowIndex).html(
				$("#codeType option:selected").text());
	}
	$("#guaranteeAmount_td" + rowIndex).html(
			$("#guaranteeAmount").attr("value"));
	$("#upperBase_td" + rowIndex).html($("#upper").attr("value"));
	$("#lowerBase_td" + rowIndex).html($("#lower").attr("value"));
	$("#deductRateBase_td" + rowIndex).html($("#deductRate").attr("value"));
	$("#startDateBase_td" + rowIndex).html($("#startDate_base").attr("value"));
	$("#endDateBase_td" + rowIndex).html($("#endDate_base").attr("value"));
	$("#ok").attr("data-dismiss", "modal");
}
/*******************************
 * 销售分成及结算封装json
 *******************************/
function baseToJson(rowIndex) {
	var json = "";
	var assbaseitems_id = $("#assbaseitems_id" + rowIndex).attr("value");
	var iscommGuarantee = $("#iscommGuarantee" + rowIndex).attr("value");
	var isGuarantee = $("#isGuarantee_base" + rowIndex).attr("value");
	var guaranteeType = $("#guaranteeType_base" + rowIndex).attr("value");
	var guaranteeAmount = $("#guaranteeAmount_base" + rowIndex).attr("value");
	var codeType = $("#codeType_base" + rowIndex).attr("value");
	var lower = $("#lower_base" + rowIndex).attr("value");
	var upper = $("#upper_base" + rowIndex).attr("value");
	var startDate = $("#startDate_base" + rowIndex).attr("value");
	var endDate = $("#endDate_base" + rowIndex).attr("value");
	var validType = $("#validType_base" + rowIndex).attr("value");
	var countBase = $("#countBase_base" + rowIndex).attr("value");
	var deductType = $("#deductType_base" + rowIndex).attr("value");
	var payDate = $("#payDate_base" + rowIndex).attr("value");

	var deductRate = $("#deductRate_base" + rowIndex).attr("value");
	var barndId = $("#barndId_base" + rowIndex).attr("value");
	json += "{'id':'" + assbaseitems_id + "','isGuarantee':'" + isGuarantee
			+ "',"
	json += "'guaranteeType':'" + guaranteeType + "','guaranteeAmount':'"
			+ guaranteeAmount + "','startDate':'" + startDate + "','endDate':'"
			+ endDate + "',"
	json += "'validType':'" + validType + "','deductType':'" + deductType
			+ "',"
	json += "'deductRate':'" + deductRate + "','countBase':'" + countBase
			+ "','codeType':'" + codeType + "',"
	json += "'lower':'" + lower + "','upper':'" + upper + "','payDate':'"
			+ payDate + "','brandsId':'" + barndId + "','iscommGuarantee':'"
			+ iscommGuarantee + "'"
	json += "},"
	return json;
}

/*******************************
 * 合同条款更新
 *******************************/
function updateItems(rowIndex) {
	//$("#assitems_id" + rowIndex).val($("#assitems_id").attr("value"));
	$("#deductCate_items" + rowIndex).val($("#deductCate").attr("value"));
	$("#amount_items" + rowIndex).val($("#amount").attr("value"));
	$("#startDate_items" + rowIndex).val($("#startDate_items").attr("value"));
	$("#endDate_items" + rowIndex).val($("#endDate_items").attr("value"));
	$("#validType_items" + rowIndex).val($("#validType_items").attr("value"));
	$("#countBase_items" + rowIndex).val($("#countBase").attr("value"));
	$("#deductType_items" + rowIndex).val($("#deductType").attr("value"));
	$("#deductCode_items" + rowIndex).val($("#deductCode").attr("value"));
	$("#taxRate_items" + rowIndex).val($("#taxRate_items").attr("value"));
	$("#deductItem_items" + rowIndex).val($("#deductItem").attr("value"));
	$("#taxRateCode_items" + rowIndex).val($("#taxRateCode_items").attr("value"));
	$("#remarks_items" + rowIndex).val($("#remarks_items").attr("value"));
	// TD
	$("#deductCodeIteme_td" + rowIndex).html($("#deductCode").attr("value"))
	$("#deductItem_td" + rowIndex).html($("#deductItem").attr("value"))
	$("#amountIteme_td" + rowIndex).html($("#amount").attr("value"))
	$("#startDateitems_td" + rowIndex)
			.html($("#startDate_items").attr("value"))
	$("#endDateitems_td" + rowIndex).html($("#endDate_items").attr("value"))

	if ($("#validType_items").attr("value") == null || $("#validType_items").attr("value") == "") {
		$("#validTypeitems_td" + rowIndex).html("");
	} else {
		$("#validTypeitems_td" + rowIndex).html(
				$("#validType_items option:selected").text());
	}
	if ($("#countBase").attr("value") == null
			|| $("#countBase").attr("value") == "") {
		$("#countBaseitems_td" + rowIndex).html("")
	} else {
		$("#countBaseitems_td" + rowIndex).html(
				$("#countBase option:selected").text())
	}
	
	$("#taxRateitems_td" + rowIndex).html($("#taxRateCode_items").find("option:selected").text())
	
	$("#ok_items").attr("data-dismiss", "modal");
}
/*******************************
 * 合同条款拼装json
 *******************************/
function itemsToJson(rowIndex) {
	var json = "";
	var assitems_id = $("#assitems_id" + rowIndex).attr("value");
	var deductItem = $("#deductItem_items" + rowIndex).attr("value");
	var taxRate = $("#taxRate_items" + rowIndex).attr("value");

	var deductCate = $("#deductCate_items" + rowIndex).attr("value");
	var amount = $("#amount_items" + rowIndex).attr("value");
	var startDate = $("#startDate_items" + rowIndex).attr("value");
	var endDate = $("#endDate_items" + rowIndex).attr("value");
	var validType = $("#validType_items" + rowIndex).attr("value");
	var countBase = $("#countBase_items" + rowIndex).attr("value");
	var deductType = $("#deductType_items" + rowIndex).attr("value");
	var deductCode = $("#deductCode_items" + rowIndex).attr("value");
	var taxRateCode = $("#taxRateCode_items" + rowIndex).attr("value");
	var remarks = $("#remarks_items" + rowIndex).val();
	json += "{'id':'" + assitems_id + "','deductCate':'" + deductCate + "',"
	json += "'amount':'" + amount + "','startDate':'" + startDate
			+ "','endDate':'" + endDate + "',"
	json += "'validType':'" + validType + "','deductType':'" + deductType
			+ "',"
	json += "'taxRateCode':'" + taxRateCode + "','countBase':'" + countBase
			+ "','deductCode':'" + deductCode + "'"
	json += "},"

	return json;

}

/*******************************
 * 品牌更新
 *******************************/
function updateBrand(rowIndex) {
	$("#brandId" + rowIndex).val($("#brandId").attr("value"));
	$("#brandNo" + rowIndex).val($("#brandNo").attr("value"));
	$("#brandCName" + rowIndex).val($("#brandCName").attr("value"));
	$("#brandEName" + rowIndex).val($("#brandEName").attr("value"));
	$("#brandLevel" + rowIndex).val($("#brandLevel").attr("value"));
	$("#brandAttribute" + rowIndex).val($("#brandAttribute").attr("value"));
	$("#brandInviteHead" + rowIndex).val($("#brandInviteHead").attr("value"));
	$("#commodityCate" + rowIndex).val($("#commodityCate").attr("value"));
	$("#inviteCate" + rowIndex).val($("#inviteCate").attr("value"));
	$("#shop" + rowIndex).val($("#shop").attr("value"));
	$("#shopId" + rowIndex).val($("#shopId").attr("value"));
	$("#memberRateType" + rowIndex).val($("#memberRateType").attr("value"));
	$("#gcardDiscount" + rowIndex).val($("#gcardDiscount").attr("value"));
	$("#gcardDiscount2" + rowIndex).val($("#gcardDiscount2").attr("value"));
	$("#ocardDiscount2" + rowIndex).val($("#ocardDiscount2").attr("value"));
	$("#ocardDiscount" + rowIndex).val($("#ocardDiscount").attr("value"));
	$("#mainBarnd" + rowIndex).val($("#mainBarnd").val());
	$("#purchasingGroup" + rowIndex).val($("#purchasingGroup").attr("value"));
	$("#assShopIds" + rowIndex).val($("#assShopIds").val());
	$("#shopNames" + rowIndex).val($("#shopName").attr("value"));
	$("#iszs" + rowIndex).val($("input[name='is_zs']:checked").val());
	// --------------------------------------------------------------------------

	$("#brandCName_td" + rowIndex).html($("#brandCName").attr("value"));
	$("#brandEName_td" + rowIndex).html($("#brandEName").attr("value"));
	$("#inviteCate_td" + rowIndex).html($("#inviteCateName").attr("value"));
	$("#commodityCate_td" + rowIndex).html($("#commodityCate").attr("value"));
	$("#brandInviteHead_td" + rowIndex).html(
			$("#brandInviteHead").attr("value"));
	$("#shop_td" + rowIndex).html($("#shopName").attr("value"));

	// $("#shop_td"+rowIndex).html(getOptionText());
	$("#ok2").attr("data-dismiss", "modal");
}

/*******************************
 * 品牌封装json
 *******************************/
function brandToJson(rowIndex) {
	var jsonStr = "";
	var brandId = $("#brandId" + rowIndex).attr("value");
	var brandNo = $("#brandNo" + rowIndex).attr("value");
//	var brandCName = $("#brandCName" + rowIndex).attr("value");
//	var brandEName = $("#brandEName" + rowIndex).attr("value");
	var brandLevel = $("#brandLevel" + rowIndex).attr("value");
	var brandAttribute = $("#brandAttribute" + rowIndex).attr("value");
	var brandInviteHead = $("#brandInviteHead" + rowIndex).attr("value");
	var commodityCate = $("#commodityCate" + rowIndex).attr("value");
	var inviteCate = $("#inviteCate" + rowIndex).attr("value");
	var shop = $("#shop" + rowIndex).attr("value");
	var shopId = $("#shopId" + rowIndex).attr("value");
	var assbrand_id = $("#assbrand_id" + rowIndex).attr("value");
	var memberRateType = $("#memberRateType" + rowIndex).attr("value");
	var gcardDiscount = $("#gcardDiscount" + rowIndex).attr("value");
	var gcardDiscount2 = $("#gcardDiscount2" + rowIndex).attr("value");
	var ocardDiscount2 = $("#ocardDiscount2" + rowIndex).attr("value");
	var ocardDiscount = $("#ocardDiscount" + rowIndex).attr("value");
	var mainBarnd = $("#mainBarnd" + rowIndex).attr("value");
	var purchasingGroup = $("#purchasingGroup" + rowIndex).attr("value");
	jsonStr += "{'id':'" + assbrand_id + "','brandAttribute':'" + brandAttribute + "','brandNo':'"
			+ brandNo + "',"
	jsonStr += "'brandId':'" + brandId + "','brandLevel':'" + brandLevel
			+ "','memberRateType':'" + memberRateType + "',"
	jsonStr += "'gcardDiscount':'" + gcardDiscount + "','gcardDiscount2':'"
			+ gcardDiscount2 + "','ocardDiscount2':'" + ocardDiscount2
			+ "','ocardDiscount':'" + ocardDiscount + "',"
	jsonStr += "'commodityCate':'" + commodityCate + "','inviteCate':'"
			+ inviteCate + "','brandInviteHead':'" + brandInviteHead
			+ "','mainBarnd':'" + mainBarnd + "','purchasingGroup':'"
			+ purchasingGroup + "'";
	jsonStr += "},";
	return jsonStr;
}


/*******************************
 * 合同销售分成及结算添加
 *******************************/
function btnBaseOk_click() {
	var contractType = $("#contract_type").val();
	var baseWherHight = $("#contractBaseWhere_tab").height();
	var iscommGuarantee = $("#iscommGuarantee").attr("value");
	var isGuarantee = $("#isGuarantee").attr("value");
	var guaranteeType = $("#guaranteeType").attr("value");
	var guaranteeAmount = $("#guaranteeAmount").attr("value");
	var codeType = $("#codeType").attr("value");
	var lower = $("#lower").attr("value");
	var upper = $("#upper").attr("value");
	var startDate = $("#startDate_base").attr("value");
	var endDate = $("#endDate_base").attr("value");
	var validType = $("#validType_base").attr("value");
	var countBase = $("#countbase_base").attr("value");
	var deductType = $("#deductType_base").attr("value");
	var isModifyContract =$("#isModifyContract").attr("value");
	var status ='00'
	if(isModifyContract=='1'){
		var status ='01'
	}

	if (contractType == "leaseassociated") {
		var payDate = $("#payDate_base").attr("value");
		var isCompare = $("#isCompare").attr("value");
	}

	var remarks = $("#remarks_base").attr("value");
	var deductRate = $("#deductRate").attr("value");
	var barndId_base = $("#brandName").attr("value");
	var brandHight = $("#contractBrand_tab").height();
	
	
	if(startDate< $("#startDate").attr("value")){
		 $("#startDate_base").focus();
		top.$.jBox.tip('条款开始开始日期大于等于合同日期', 'warning');
		return;
	}
	
	if(endDate> $("#endDate").attr("value")){
		 $("#endDate_base").focus();
		 top.$.jBox.tip('条款结束日期小于等于合同结束日期', 'warning');
		return;
	}
	if (barndId_base == "" || barndId_base == 0) {
		$("#brandName").focus();
		top.$.jBox.tip('品牌不能为空', 'warning');
		return;
	}
	if (isGuarantee == "") {
		$("#isGuarantee").focus();
		top.$.jBox.tip('是否保底不能为空', 'warning');
		return;
	}

	if (isGuarantee == "1") {
		if (guaranteeType == "") {
			$("#guaranteeType").focus();
			top.$.jBox.tip('保底形式不能为空', 'warning');
			return;
		}
		if (guaranteeAmount == "") {
			$("#guaranteeAmount").focus();
			top.$.jBox.tip('保底金额不能为空', 'warning');
			return;
		}
		if (countBase == "") {
			$("#countBase").focus();
			top.$.jBox.tip('销售是否计入保底不能为空', 'warning');
			return;
		}
		if (iscommGuarantee == "") {
			$("#iscommGuarantee").focus();
			top.$.jBox.tip('通用保底不能为空', 'warning');
			return;
		}
	}
	if (codeType == "") {
		$("#codeType").focus();
		top.$.jBox.tip('码类型不能为空', 'warning');
		return;
	}
	// if (lower==""){
	// $("#lower").focus();
	// top.$.jBox.tip('扣款下线不能为空','warning');
	// return;
	// }
	// if (upper==""){
	// $("#upper").focus();
	// top.$.jBox.tip('扣款上线不能为空','warning');
	// return;
	// }
	if (startDate == "") {
		$("#startDate").focus();
		top.$.jBox.tip('开始时间不能为空', 'warning');
		return;
	}
	if (endDate == "") {
		$("#endDate").focus();
		top.$.jBox.tip('结束时间不能为空', 'warning');
		return;
	}
	if(endDate <= startDate ){
		$("#endDate").focus();
		top.$.jBox.tip('结束时间不能早于开始时间,请重新输入!', 'warning');
		return;
	}
	if (validType == "") {
		$("#validType").focus();
		top.$.jBox.tip('时效类型不能为空', 'warning');
		return;
	}
	if (deductType == "") {
		$("#deductType").focus();
		top.$.jBox.tip('扣款方式不能为空', 'warning');
		return;
	}
	if (payDate == "" && contractType == "leaseassociated") {
		$("#payDate").focus();
		top.$.jBox.tip('租金支付日不能为空', 'warning');
		return;
	}
	if (deductRate == "") {
		$("#deductRate").focus();
		top.$.jBox.tip('扣率不能为空', 'warning');
		return;
	}
	// 动态添加行
	if ($("#operate_base").attr("value") == 'edit') {
		updateBase($("#base_seq").attr("value"));

	} else {

		var newRow = document.createElement("tr");
		var newTd0 = document.createElement("td");
		newTd0.setAttribute("class", "o-l-tab-ckb");
		// var newTd1 = document.createElement ("td");
		// newTd1.setAttribute ("class","o-l-tab-ckb");
		// newTd1.setAttribute ("id","isCompareBase_td"+num); //
		var newTd1 = document.createElement("td");
		newTd1.setAttribute("id", "isGuaranteeBase_td" + num_base); //
		newTd1.setAttribute("class", "o-l-tab-ckb");
		var newTd2 = document.createElement("td");
		newTd2.setAttribute("id", "guaranteeAmount_td" + num_base); //
		newTd2.setAttribute("class", "o-l-tab-ckb");
		var newTd3 = document.createElement("td");
		newTd3.setAttribute("class", "o-l-tab-ckb");
		newTd3.setAttribute("id", "lowerBase_td" + num_base); //
		var newTd4 = document.createElement("td");
		newTd4.setAttribute("class", "o-l-tab-ckb");
		newTd4.setAttribute("id", "upperBase_td" + num_base); //
		var newTd5 = document.createElement("td");
		newTd5.setAttribute("class", "o-l-tab-ckb");
		newTd5.setAttribute("id", "deductRateBase_td" + num_base); //
		var newTd6 = document.createElement("td");
		newTd6.setAttribute("class", "o-l-tab-ckb");
		newTd6.setAttribute("id", "startDateBase_td" + num_base); //
		var newTd7 = document.createElement("td");
		newTd7.setAttribute("class", "o-l-tab-ckb");
		newTd7.setAttribute("id", "endDateBase_td" + num_base); //
		var newTd8 = document.createElement("td");
		newTd8.setAttribute("class", "o-l-tab-ckb");
		newTd8.setAttribute("id", "endDateBase_td" + num_base); //
		if (contractType == "leaseassociated") {
			var newTd9 = document.createElement("td");
			newTd9.setAttribute("class", "o-l-tab-ckb");
			newTd9.setAttribute("id", "isCompareBase_td" + num);
		}
		newTd0.innerHTML = '<div class="input-group">'
				+ '<input type="hidden" name="assbaseitemsBox"  value="'
				+ num_base
				+ '" />'
				+ '<input type="hidden" id="assbaseitems_id'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base
				+ '].id" /></div>'
				+ '<input type="hidden" class="form-control"  id="iscommGuarantee'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base
				+ '].iscommGuarantee"  value="'
				+ iscommGuarantee
				+ '"   />'
				+ '<input type="hidden" class="form-control"  id="isGuarantee_base'
				+ num_base
				+ '"  name="assbaseitemsList['
				+ num_base
				+ '].isGuarantee"  value="'
				+ isGuarantee
				+ '"    />'
				+ '<input type="hidden" class="form-control"  id="guaranteeType_base'
				+ num_base
				+ '"  name="assbaseitemsList['
				+ num_base
				+ '].guaranteeType"   value="'
				+ guaranteeType
				+ '"  />'
				+ '<input type="hidden" class="form-control"  id="guaranteeAmount_base'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base
				+ '].guaranteeAmount"   value="'
				+ guaranteeAmount
				+ '"   />'
				+ '<input type="hidden" class="form-control"  id="codeType_base'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base
				+ '].codeType"  value="'
				+ codeType
				+ '" />'
				+ '<input type="hidden" class="form-control"  id="lower_base'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base
				+ '].lower" value="'
				+ lower
				+ '"   />'
				+ '<input type="hidden" class="form-control"  id="upper_base'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base
				+ '].upper"  value="'
				+ upper
				+ '"   />'
				+ '<input type="hidden" class="form-control"  id="startDate_base'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base
				+ '].startDate"  value="'
				+ startDate
				+ '"  />'
				+ '<input type="hidden" class="form-control"  id="endDate_base'
				+ num_base
				+ '"  name="assbaseitemsList['
				+ num_base
				+ '].endDate"   value="'
				+ endDate
				+ '"  />'
				+ '<input type="hidden" class="form-control"  id="validType_base'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base
				+ '].validType" value="'
				+ validType
				+ '"  />'
				+ '<input type="hidden" class="form-control" id="countBase_base'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base
				+ '].countBase"   value="'
				+ countBase
				+ '"   />'
				+ '<input type="hidden" class="form-control" id="deductType_base'
				+ num_base
				+ '"  name="assbaseitemsList['
				+ num_base
				+ '].deductType"  value="'
				+ deductType
				+ '"  />'
				+ '<input type="hidden" class="form-control" id="payDate_base'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base
				+ '].payDate"  value="'
				+ payDate
				+ '"   />'
				+ '<input type="hidden" class="form-control" id="remarks_base'
				+ num_base
				+ '"  name="assbaseitemsList['
				+ num_base
				+ '].remarks" value="'
				+ remarks
				+ '"   />'
				+ '<input type="hidden" class="form-control" id="deductRate_base'
				+ num_base
				+ '"  name="assbaseitemsList['
				+ num_base
				+ '].deductRate" value="'
				+ deductRate
				+ '"  />'
				+ '<input type="hidden" class="form-control" id="barndId_base'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base
				+ '].brandsId" value="'
				+ barndId_base
				+ '"  />'
				+ '<input type="hidden" class="form-control" id="isCompare_base'
				+ num_base
				+ '" name="assbaseitemsList['
				+ num_base+'].isCompare" value="' + isCompare + '"  />'
				+ '<input type="hidden" class="form-control" id="status0'+num_base+'" name="assbaseitemsList['+num_base+'].status" value="'+status+'"  />'

		if (isGuarantee == null || isGuarantee == "") {
			newTd1.innerHTML = "";
		} else {
			newTd1.innerHTML = $("#isGuarantee option:selected").text();
		}

		newTd2.innerHTML = guaranteeAmount;

		newTd3.innerHTML = lower;
		newTd4.innerHTML = upper;
		newTd5.innerHTML = deductRate;
		newTd6.innerHTML = startDate;
		newTd7.innerHTML = endDate;
		newTd8.innerHTML = '<a href="#" onclick="edit_base(this,' + num_base
				+ ')" >修改</a>|<a href="#" onclick="remove_assbaseitems(this,'
				+ num_base + ')">删除</a>';
		newRow.appendChild(newTd0);
		newRow.appendChild(newTd1);
		newRow.appendChild(newTd2);
		newRow.appendChild(newTd3);
		newRow.appendChild(newTd4);
		newRow.appendChild(newTd5);
		newRow.appendChild(newTd6);
		newRow.appendChild(newTd7);
		newRow.appendChild(newTd8);
		if (contractType == "leaseassociated") {
			newRow.appendChild(newTd9);
		}
		document.getElementById("contractBaseWhere").appendChild(newRow);
		assbaseItemsList.push(num_base);
		num_base++;
		$("#contractBaseWhere_tab").height(baseWherHight + 100)
		$("#ok").attr("data-dismiss", "modal");
	}
}

/*******************************************************************************
 * 
 * 添加修改合同条款
 * 
 ******************************************************************************/
function btnItemsOk_click() {
	var deductItem = $("#deductItem").attr("value");
	var deductCate = $("#deductCate").attr("value");
	var amount = $("#amount").attr("value");
	var startDate_items = $("#startDate_items").attr("value");
	var endDate_items = $("#endDate_items").attr("value");
	var validType = $("#validType_items").attr("value");
	var countBase_items = $("#countBase").attr("value");
	var deductType = $("#deductType").attr("value");
	var deductCode = $("#deductCode").attr("value");
	var taxRate = $("#taxRateCode_items").find("option:selected").text();
	var taxRateCode = $("#taxRateCode_items").attr("value");
	var remarks_items = $("#remarks_items").attr("value");
	
	if(startDate_items< $("#startDate").attr("value")){
		 $("#startDate_items").focus();
		top.$.jBox.tip('条款开始开始日期大于等于合同日期', 'warning');
		return;
	}
	
	if(endDate_items> $("#endDate").attr("value")){
		 $("#endDate_items").focus();
		top.$.jBox.tip('条款结束日期小于等于合同结束日期', 'warning');
		return;
	}
	
	if (deductCode == "") {
		$("#deductCode").focus();
		top.$.jBox.tip('扣款项目代码不能为空', 'warning');
		return;
	}
	if (deductItem == "") {
		$("#deductItem").focus();
		top.$.jBox.tip('扣款项不能为空', 'warning');
		return;
	}
	if (startDate_items == "") {
		$("#startDate_items").focus();
		top.$.jBox.tip('开始日期不能为空', 'warning');
		return;

	}
	if (endDate_items == "") {
		$("#endDate_items").focus();
		top.$.jBox.tip('结束日期不能为空', 'warning');
		return;

	}
	if (endDate_items <= startDate_items) {
		$("#endDate_items").focus();
		top.$.jBox.tip('结束日期不能早于开始日期,请重新输入', 'warning');
		return;
	}
	
	if (amount == "") {
		$("#amount").focus();
		top.$.jBox.tip('金额不能为空', 'warning');
		return;
	}
	if (validType == "") {
		$("#validType").focus();
		top.$.jBox.tip('时效类型不能为空', 'warning');
		return;

	}
	$("#ok").attr("data-dismiss", "modal");

	if ($("#operate_item").attr("value") == 'edit') {
		updateItems($("#item_seq").attr("value"));

	} else {

		var commnWhereHight = $("#contractCommnWhere_tab").height();
		// 动态添加行
		var newRow = document.createElement("tr");
		// 动态创建列
		var newTd0 = document.createElement("td");
		newTd0.setAttribute("class", "o-l-tab-ckb");
		var newTd1 = document.createElement("td");
		newTd1.setAttribute("class", "o-l-tab-ckb");
		newTd1.setAttribute("id", "deductCodeIteme_td" + num_items);
		var newTd2 = document.createElement("td");
		newTd2.setAttribute("class", "o-l-tab-ckb");
		newTd2.setAttribute("id", "deductItem_td" + num_items);
		var newTd3 = document.createElement("td");
		newTd3.setAttribute("class", "o-l-tab-ckb");
		newTd3.setAttribute("id", "amountIteme_td" + num_items);
		var newTd4 = document.createElement("td");
		newTd4.setAttribute("class", "o-l-tab-ckb");
		newTd4.setAttribute("id", "startDateitems_td" + num_items);
		var newTd5 = document.createElement("td");
		newTd5.setAttribute("class", "o-l-tab-ckb");
		newTd5.setAttribute("id", "endDateitems_td" + num_items);
		var newTd6 = document.createElement("td");
		newTd6.setAttribute("id", "validTypeitems_td" + num_items);
		newTd6.setAttribute("class", "o-l-tab-ckb");
		var newTd7 = document.createElement("td");
		newTd7.setAttribute("id", "countBaseitems_td" + num_items);
		newTd7.setAttribute("class", "o-l-tab-ckb");
		var newTd8 = document.createElement("td");
		newTd8.setAttribute("id", "taxRateitems_td" + num_items);
		newTd8.setAttribute("class", "o-l-tab-ckb");

		var newTd9 = document.createElement("td");
		newTd9.setAttribute("id", "operate_td" + num_items);
		newTd9.setAttribute("class", "o-l-tab-ckb");

		// 动态添加内容

		newTd0.innerHTML = '<input type="hidden" name="assitemsBox" value="'
				+ num_items + '" />' + '<input type="hidden" id="assitems_id'
				+ num_items + '"  />'
				+ '<input type="hidden" id="deductCode_items' + num_items
				+ '" name="assitemsList[' + num_items + '].deductCode" value="'
				+ deductCode + '" />'
				+ '<input type="hidden" id="deductItem_items' + num_items
				+ '"  name="assitemsList[' + num_items
				+ '].deductItem" value="' + deductItem + '" />'
				+ '<input type="hidden" id="deductCate_items' + num_items
				+ '"  name="assitemsList[' + num_items
				+ '].deductCate" value="' + deductCate + '" />'
				+ '<input type="hidden" id="amount_items' + num_items
				+ '"  name="assitemsList[' + num_items + '].amount" value="'
				+ amount + '" />' + '<input type="hidden" id="startDate_items'
				+ num_items + '"  name="assitemsList[' + num_items
				+ '].startDate" value="' + startDate_items + '" />'
				+ '<input type="hidden" id="endDate_items' + num_items
				+ '"  name="assitemsList[' + num_items + '].endDate" value="'
				+ endDate_items + '" />'
				+ '<input type="hidden" id="validType_items' + num_items
				+ '"  name="assitemsList[' + num_items + '].validType" value="'
				+ validType + '" />'
				+ '<input type="hidden" id="countBase_items' + num_items
				+ '"  name="assitemsList[' + num_items + '].countBase" value="'
				+ countBase_items + '" />'
				+ '<input type="hidden" id="deductType_items' + num_items
				+ '"  name="assitemsList[' + num_items
				+ '].deductType" value="' + deductType + '" />'
				+ '<input type="hidden" id="taxRate_items' + num_items
				+ '"  name="assitemsList[' + num_items + '].taxRate" value="'
				+ taxRate + '" />' + '<input type="hidden" id="remarks_items'
				+ num_items + '"  name="assitemsList[' + num_items
				+ '].remarks" value="' + remarks_items + '" />'
				+ '<input type="hidden" id="taxRateCode_items' + num_items
				+ '"  name="assitemsList[' + num_items
				+ '].taxRateCode" value="' + taxRateCode + '" />'

		newTd1.innerHTML = deductCode;
		newTd2.innerHTML = deductItem;
		newTd3.innerHTML = amount;
		newTd4.innerHTML = startDate_items;
		newTd5.innerHTML = endDate_items;
		if (validType == null || validType == "") {
			newTd6.innerHTML = "";
		} else {
			newTd6.innerHTML = $("#validType_items option:selected").text();
		}
		if (countBase_items == null || countBase_items == "") {
			newTd7.innerHTML = "";
		} else {
			newTd7.innerHTML = $("#countBase option:selected").text();
		}
		newTd8.innerHTML = taxRate;
		newTd9.innerHTML = '<a href="#" onclick="edit_assitems(this,'
				+ num_items
				+ ')" >修改</a>|<a href="#" onclick="remove_assitems(this,'
				+ num_items + ')">删除</a>'
		newRow.appendChild(newTd0);
		newRow.appendChild(newTd1);
		newRow.appendChild(newTd2);
		newRow.appendChild(newTd3);
		newRow.appendChild(newTd4);
		newRow.appendChild(newTd5);
		newRow.appendChild(newTd6);
		newRow.appendChild(newTd7);
		newRow.appendChild(newTd8);
		newRow.appendChild(newTd9);

		document.getElementById("contractCommnWhere").appendChild(newRow);
		itemList.push(num_items);
		num_items++;
		$("#contractCommnWhere_tab").height(commnWhereHight + 100);
		$("#ok_items").attr("data-dismiss", "modal");
	}
}


/*******************************
 * 品牌添加
 *******************************/
function btnBrandOk_click() {
	var brandId = $("#brandId").attr("value");
	var brandNo = $("#brandNo").attr("value");
	var brandCName = $("#brandCName").attr("value");
	var brandEName = $("#brandEName").attr("value");
	var brandLevel = $("#brandLevel").attr("value");
	var brandAttribute = $("#brandAttribute").attr("value");
	var brandInviteHead = $("#brandInviteHead").attr("value");
	var commodityCate = $("#commodityCate").attr("value");
	var inviteCate = $("#inviteCate").attr("value");
	var inviteCateName = $("#inviteCateName").attr("value");
	var shopName = $("#shopName").attr("value");
	var shopId = $("#assShopIds").val();
	var assbrand_id = $("#assbrand_id").attr("value");
	var memberRateType = $("#memberRateType").attr("value");
	var gcardDiscount = $("#gcardDiscount").attr("value");
	var gcardDiscount2 = $("#gcardDiscount2").attr("value");
	var ocardDiscount2 = $("#ocardDiscount2").attr("value");
	var ocardDiscount = $("#ocardDiscount").attr("value");
	var mainBrand = $("#mainBarnd").attr("value");
	var purchasingGroup = $("#purchasingGroup").attr("value");
	var iszs = $("input[name='is_zs']:checked").val();
	if (brandCName == "") {
		$("#brandCName").focus();
		top.$.jBox.tip('品牌不能为空', 'warning');
		return;
	}
	if (brandAttribute == "") {
		$("#brandAttribute").focus();
		top.$.jBox.tip('品牌属性不能为空', 'warning');
		return;
	}
	if (inviteCate == "") {
		$("#inviteCate").focus();
		top.$.jBox.tip('招商分类不能为空', 'warning');
		return;
	}
	// if(shopName ==""){
	// $("#shopName").focus();
	// top.$.jBox.tip('商铺不能为空','warning');
	// return;
	// }
	if (mainBrand == "") {
		$("#mainBrand").focus();
		top.$.jBox.tip('是否主品牌不能为空', 'warning');
		return;
	}
	if (memberRateType == "") {
		$("#memberRateType").focus();
		top.$.jBox.tip('会员卡折率类型不能为空', 'warning');
		return;
	}
	if (memberRateType == "20" && gcardDiscount == "") {
		$("#gcardDiscount").focus();
		top.$.jBox.tip('金卡折率不能为空', 'warning');
		return;
	}
	if (memberRateType == "20" && ocardDiscount == "") {
		$("#ocardDiscount").focus();
		top.$.jBox.tip('普卡折率不能为空', 'warning');
		return;
	}

	if ($("#operate").attr("value") == 'edit') {
		updateBrand($("#barnd_seq").attr("value"));

	} else {

		var brandHight = $("#contractBrand_tab").height();
		var newRow = document.createElement("tr");
		var newTd0 = document.createElement("td");
		newTd0.setAttribute("class", "o-l-tab-ckb");

		var newTd1 = document.createElement("td");
		newTd1.setAttribute("class", "o-l-tab-ckb");
		newTd1.setAttribute("id", "brandCName_td" + num);
		var newTd2 = document.createElement("td");
		newTd2.setAttribute("class", "o-l-tab-ckb");
		newTd2.setAttribute("id", "brandEName_td" + num);
		var newTd3 = document.createElement("td");
		newTd3.setAttribute("id", "inviteCate_td" + num);
		newTd3.setAttribute("class", "o-l-tab-ckb");
		var newTd4 = document.createElement("td");
		newTd4.setAttribute("id", "brandInviteHead_td" + num);
		newTd4.setAttribute("class", "o-l-tab-ckb");
		var newTd5 = document.createElement("td");
		newTd5.setAttribute("id", "shop_td" + num);
		newTd5.setAttribute("class", "o-l-tab-ckb");
		var newTd6 = document.createElement("td");
		newTd6.setAttribute("id", "operate_td" + num);
		newTd6.setAttribute("class", "o-l-tab-ckb");
		// 动态添加内容
		newTd0.innerHTML = '<input type="hidden" name="assbrandBox" value="'
				+ num + '"  />' + '<input type="hidden" id="assbrand_id' + num
				+ '"  name="assbrandsList[' + num + '].id" value="'
				+ assbrand_id + '" />' + '<input type="hidden" id="brandNo'
				+ num + '" name="assbrandsList[' + num + '].brandNo" value="'
				+ brandNo + '"/>' + '<input type="hidden" id="brandId' + num
				+ '" name="assbrandsList[' + num + '].brandId" value="'
				+ brandId + '"/>' + '<input type="hidden" id="brandCName' + num
				+ '" name="assbrandsList[' + num + '].brandCName" value="'
				+ brandCName + '"/>' + '<input type="hidden" id="brandEName'
				+ num + '" name="assbrandsList[' + num
				+ '].brandEName" value="' + brandEName + '" />'
				+ '<input type="hidden" id="brandLevel' + num
				+ '" name="assbrandsList[' + num + '].brandLevel" value="'
				+ brandLevel + '" />'
				+ '<input type="hidden" id="brandAttribute' + num
				+ '" name="assbrandsList[' + num + '].brandAttribute" value="'
				+ brandAttribute + '" />'
				+ '<input type="hidden" id="inviteCate' + num
				+ '" name="assbrandsList[' + num + '].inviteCate" value="'
				+ inviteCate + '" />'
				+ '<input type="hidden" id="commodityCate' + num
				+ '" name="assbrandsList[' + num + '].commodityCate" value="'
				+ commodityCate + '" />'
				+ '<input type="hidden" id="brandInviteHead' + num
				+ '" name="assbrandsList[' + num + '].brandInviteHead" value="'
				+ brandInviteHead + '" />'
				// +'<input type="hidden" id="shop'+num+'" value="'+shop+'" />'
				+ '<input type="hidden" id="memberRateType' + num
				+ '" name="assbrandsList[' + num + '].memberRateType" value="'
				+ memberRateType + '" />'
				+ '<input type="hidden" id="gcardDiscount' + num
				+ '" name="assbrandsList[' + num + '].gcardDiscount" value="'
				+ gcardDiscount + '" />'
				+ '<input type="hidden" id="gcardDiscount2' + num
				+ '" name="assbrandsList[' + num + '].gcardDiscount2" value="'
				+ gcardDiscount2 + '" />'
				+ '<input type="hidden" id="ocardDiscount2' + num
				+ '" name="assbrandsList[' + num + '].ocardDiscount2" value="'
				+ ocardDiscount2 + '" />'
				+ '<input type="hidden" id="ocardDiscount' + num
				+ '" name="assbrandsList[' + num + '].ocardDiscount" value="'
				+ ocardDiscount + '" />' + '<input type="hidden" id="mainBarnd'
				+ num + '" name="assbrandsList[' + num + '].mainBarnd" value="'
				+ mainBrand + '" />'
				+ '<input type="hidden" id="purchasingGroup' + num
				+ '"  name="assbrandsList[' + num
				+ '].purchasingGroup" value="' + purchasingGroup + '" />'
				+ '<input type="hidden" id="assShopIds' + num
				+ '"  name="assbrandsList[' + num + '].assShopIds" value="'
				+ shopId + '" />' + '<input type="hidden" id="shopNames' + num
				+ '"  name="assbrandsList[' + num + '].shopNames" value="'
				+ shopName + '" />' + '<input type="hidden" id="iszs' + num
				+ '"  name="assbrandsList[' + num + '].iszs" value="'
				+ iszs + '" />'
		newTd1.innerHTML = brandCName;
		newTd2.innerHTML = brandEName;
		newTd3.innerHTML = inviteCateName;
		newTd4.innerHTML = brandInviteHead;
		newTd5.innerHTML = shopName;
		newTd6.innerHTML = '<a href="#" onclick="edit_brand(this,' + num
				+ ')" >修改</a>|<a href="#" onclick="remove_brand(this,' + num
				+ ')">删除</a>'
		newRow.appendChild(newTd0);
		newRow.appendChild(newTd1);
		newRow.appendChild(newTd2);
		newRow.appendChild(newTd3);
		newRow.appendChild(newTd4);
		newRow.appendChild(newTd5);
		newRow.appendChild(newTd6);
		document.getElementById("tableBrand").appendChild(newRow);
		list.push(num);
		num++;

		$("#contractBrand_tab").height(brandHight + 100)
		$("#ok2").attr("data-dismiss", "modal");
	}

}

// 获得选中铺位的value
function getCheckedValue(checkName) {
	var strValue = "";
	$("input[name='" + checkName + "']:checkbox").each(function() {
		if ($(this).attr("checked")) {
			strValue += $(this).val() + ",";
		}
	});
	if (strValue != null && strValue != "") {
		strValue = strValue.substring(0, strValue.length - 1);
	}
	return strValue;
}

/*******************************
 * 税率变更
 *******************************/
function taxRateCodeChange(value, text) {
	$("#taxRateCode").val(value);
	$("#taxRate").val(text);
}

/*******************************
 * 税率类型变更
 *******************************/
function taxTypeChange(value, text) {
	var contextPath = Com_Parameter.ContextPath;
	if (value == null || value == "") {
		$("#taxRateCode_select").empty();
	} else {
		$("#taxTypeCode").val(value);
		$("#taxType").val(text);
		$("#taxRateCode_select").FillOptions(
				contextPath + "/biz/contract/commonContract/" + value
						+ "/getTaxRateTypeCode", {
					datatype : "json",
					textfield : "taxRate",
					valuefiled : "taxRateNum",
					defValue : taxRateCode
				});
		
		var val = $("#taxRateCode_select").val();
		$("#taxRateCode").val(val);
		var valName = $("#taxRateCode_select").find("option:selected").text();
		$("#taxRate").val(valName);
	}
}

/*******************************
 * 临时条款添加
 *******************************/
function add_hidden(tId, tList) {
	var tr = $(tId).clone();
	var table = $(tId).closest("table");
	// 替换所有的hidden，方便spring的form绑定
	var inputs = tr.find("input");
	var selects = tr.find("select");
	if (tId == "#hidden_contact") {
		max_tr_contact++;
	}
	inputs.each(function() {
		if (tId == "#hidden_contact") {
			this.id = tList + max_tr_contact + this.id;
			this.name = tList + "[" + max_tr_contact + "]" + this.name;
			$(this).attr("data_index", "" + max_tr_contact + "");
		}

	});
	selects.each(function() {
		if (tId == "#hidden_contact") {
			var select = this;
			var ui = $(this);
			if (this.id == '.itemCode') {
				ui.FillOptions(Com_Parameter.ContextPath
						+ "/sys/base/spfItem/getSpecificItems", {
					datatype : "json",
					textfield : "itemName",
					valuefiled : "itemNum"
				});
			}
			this.id = tList + max_tr_contact + this.id;
			this.name = tList + "[" + max_tr_contact + "]" + this.name;
			$(this).attr("data_index", "" + max_tr_contact + "");
		}

	});
	tr.show();
	table.append(tr);
}

/*******************************
 * 临时条款删除
 *******************************/
function delete_tr(t, dList) {
	var tr = $(t).closest("tr");
	var intputss = tr.find("input :first")
	var id = tr.find("input :first").val();
	if (id != 0) {
		var val = $(dList).val();
		if (val == "") {
			$(dList).val(id);
		} else {
			$(dList).val(val + "," + id);
		}
	}
	tr.remove();

}
/**
 * 扣款项目
 * 
 * @param tr1
 * @param t
 * @param value
 * @param text
 */
function changeItem(tr1, t, value, text) {
	var tr = $(t).closest("tr");
	var cid = "";
	var selects = tr.find("select");
	selects.each(function() {
		var index = $(this).attr("data_index");
		var kid = 'assTempItemCodeList' + index + '.remark';
		var remarkUi = tr.find("input");
		remarkUi.each(function() {
			if (this.id == kid) {
				this.value = text
			}

		});
		var cid = 'assTempItemCodeList' + index + '.conditions';
		if (this.id == cid) {
			var ui = $(this);
			ui.FillOptions(Com_Parameter.ContextPath
					+ "/sys/base/spfItem/getConditions?sptNum=" + value, {
				datatype : "json",
				textfield : "value",
				valuefiled : "key"
			});
		}
	})

}

function timeStamp2String(time) {
	var datetime = new Date();
	datetime.setTime(time);
	var year = datetime.getFullYear();
	var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1)
			: datetime.getMonth() + 1;
	var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime
			.getDate();
	var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime
			.getHours();
	var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes()
			: datetime.getMinutes();
	var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds()
			: datetime.getSeconds();
	return year + "-" + month + "-" + date;
}
